package com.lbj.mvvm.work

import androidx.lifecycle.MutableLiveData
import com.lbj.mvvm.network.AppException
import com.lbj.mvvm.network.BaseResponse
import com.lbj.mvvm.network.ExceptionHandle

/**
 * 处理返回值
 * @param result 请求结果
 */
fun <T> MutableLiveData<Work<T>>.run(result: BaseResponse<T>, show: Int = 0) {
    if (show == 14) {
       value =  Work.onError(AppException(14, result.msg ?: ""))
        return
    }
    value = if (result.isSuccess()) Work.onSuccess(
        result.data,
        result.returnCode,
        result.msg ?: ""
    ) else
        Work.onError(AppException(result.returnCode, result.msg ?: ""))
}

/**
 * 不处理返回值 直接返回请求结果
 * @param result 请求结果
 */
fun <T> MutableLiveData<Work<T>>.run(result: T) {
    value = Work.onSuccess(result)
}

/**
 * 异常转换异常处理
 */
fun <T> MutableLiveData<Work<T>>.paresException(e: Throwable) {
    this.value = Work.onError(ExceptionHandle.handleException(e))
}